#include "ScriptComponent.hpp"
#include <memory>

using namespace SLB;
using namespace Typhoon;

ScriptObject::ScriptObject() { test =123;}
ScriptObject::~ScriptObject() {}

Vector::Vector() {
	x=y=0;
}
Vector::~Vector() {
}


ScriptObject* ScriptValues::getObject(const char* key) {
	return objectMap[key];
}

void ScriptValues::setObject(const char* k, ScriptObject* v) {
	objectMap[k] = v; 
}

void ScriptValues::Copy(const ScriptValues& source) {
	objectMap = source.objectMap;
}


ScriptComponent::ScriptComponent() {
	index = IGameComponent::idCounter++;
	Values = new ScriptValues();
}

ScriptComponent::~ScriptComponent() {
	delete Values;
}

void ScriptComponent::Copy(const ScriptComponent& source) {
	map = source.map;
	index = source.index;
	Values->Copy(*source.Values);
}

const char* ScriptComponent::get(const char* key) {
	return map[key];
}

void ScriptComponent::set(const char* k, const char* v) {
	map[k] = v; 
}

void ScriptComponent::InitScripting() {

	SLB::Class<ScriptValues>("Values")
		.constructor()
		//R (C::*func)(K,V)
		.object__index( &ScriptValues::getObject )
		.object__newindex( &ScriptValues::setObject )
		;

	SLB::Class<ScriptComponent>("GameComponent")
		.constructor()
		//R (C::*func)(K,V)
		.object__index( &ScriptComponent::get )
		.object__newindex( &ScriptComponent::set )
		.property("v",&ScriptComponent::Values);
		;

	SLB::Class<ScriptObject >("ScriptObject")
		.constructor()
		.property("test", &ScriptObject::test)
.convertibleTo<Vector>()//&SLB::ClassConversor<ScriptObject, Vector>::convertToDerived)
		;

	

	SLB::Class<Vector >("Vector")
		.constructor()
		.dynamic_inherits<ScriptObject>()

		.property("x", &Vector::x )
		.property("y", &Vector::y )
		;

}

int ScriptComponent::IndexToReturn = 0;

const int ScriptComponent::GetIndex() {
	return IndexToReturn;
}

int ScriptComponent::GetId() {
	return index;
}